Plugins.load( "UIKit" );
 
var window = new UIWindow( UIHardware.fullScreenApplicationContentRect );
window.setHidden( true );
window.orderFront();
window.makeKey();
window.backgroundColor = [ 1 , 1 , 1 , 0 ];
 
var mainView = new UIView();
 
window.setContentView( mainView );

var wifiIsEnabled = isEnabled();
if (wifiIsEnabled) {
	disable();
} else {
	enable();
}

onLoad = function () {
	var myTimer = new Timer(0);
	myTimer.onTimer = function () {
		this.stop();
		terminate();
	};
	myTimer.start();
};
/*var label = new UITextLabel( [ 40 , 40 , window.bounds[ 2 ] - 80 , 120 ] );
label.backgroundColor = [ 0 , 0 , 0 , 1 ];
label.text = wifiIsEnabled ? "On" : "Off" ;
label.setFont( new Font( "Trebuchet MS" , 0 , 36 ) );
label.color = [ 1 , 1 , 1 , 1 ];
label.centersHorizontally = true;

mainView.addSubview(label);

var button = new UIPushButton(label.frame);
mainView.addSubview(button);

button.onPressed = function (tapCount, fingerCount, event) {
	if (wifiIsEnabled) {
		disable();
		label.text = "Off";
	} else {
		enable();
		label.text = "On";
	}
	wifiIsEnabled = !wifiIsEnabled;
};
*/

function disable() {
	log("Bringing down...");
	executeShellCommand("/sbin/ifconfig en0 down");
}
function enable() {
	log("Bringing up...");
	executeShellCommand("/sbin/ifconfig en0 up");
}
function isEnabled() {
	var output = executeShellCommand("/sbin/ifconfig en0");
	return output.match(/inet \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} /);
}
